package com.mb.app.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mb.app.dao.TbProductPicMapper;
import com.mb.app.domain.TbProductPic;
import com.mb.app.service.ITbProductPicService;
import com.mb.common.config.CommonConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品图片  服务实现类
 * </p>
 *
 * @author liliping
 * @since 2019-04-02
 */
@Service
public class TbProductPicServiceImpl extends ServiceImpl<TbProductPicMapper, TbProductPic> implements ITbProductPicService {

    @Autowired
    private CommonConfig commonConfig;

    @Override
    public List<TbProductPic> queryList(Map<String, Object> map) {
        List<TbProductPic> list = this.baseMapper.lst(map);
        return list;
    }

    @Override
    public int count(Map<String, Object> map) {
        return this.baseMapper.countt(map);
    }

    @Override
    public int remove(Long Id, Long userId) {
        TbProductPic tbProductPic = new TbProductPic();
        tbProductPic.setId(Id);
        tbProductPic.setDeleteTime(new Date());
        tbProductPic.setDataStatus(0);
        tbProductPic.setDeleteUserId(userId);
        return this.baseMapper.updateById(tbProductPic);
    }

    @Override
    public TbProductPic getDtl(Long Id) {
        TbProductPic productPic = this.baseMapper.selectByIds(Id);
        if (productPic != null && StringUtils.isNotEmpty(productPic.getPicUrl())) {
            productPic.setPicUrl(commonConfig.getImgDomain() + productPic.getPicUrl());
        }
        if (productPic != null && StringUtils.isNotEmpty(productPic.getVideoUrl())) {
            productPic.setVideoUrl(commonConfig.getImgDomain() + productPic.getVideoUrl());
        }
        return productPic;
    }

    @Override
    public int insertSelective(TbProductPic tbProductPic) {
        return this.baseMapper.insertSelective(tbProductPic);
    }

    @Override
    public int update(TbProductPic productPic) {
        if (StringUtils.isNotEmpty(productPic.getPicUrl())) {
            String PicUrl = productPic.getPicUrl();
            PicUrl = PicUrl.replaceAll(commonConfig.getImgDomain(), "");
            productPic.setPicUrl(PicUrl);
        }
        if (StringUtils.isNotEmpty(productPic.getVideoUrl())) {
            String VideoUrl = productPic.getVideoUrl();
            VideoUrl = VideoUrl.replaceAll(commonConfig.getImgDomain(), "");
            productPic.setVideoUrl(VideoUrl);
        }
        int istre = this.baseMapper.updateById(productPic);
        return istre;
    }

    @Override
    public TbProductPic selectByIds(TbProductPic productPic) {
        return this.baseMapper.selectOne(productPic);
    }

}
